package com.tjsiqi.controller;


import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.Webusercontrol;
import com.tjsiqi.param.DeptParam;
import com.tjsiqi.param.UserParam;
import com.tjsiqi.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@Controller
@RequestMapping("/zhuce")
public class SysRegisterController {

     private static  final Logger logger = LoggerFactory.getLogger(SysRegisterController.class);
    @Resource
    SysRegisterService sysRegisterService;
    @Resource
    SysUserService sysUserService;
    @Resource
    WebUserControlService webUserControlService;
    @Resource
    SysDeptService sysDeptService;
    @Resource
    SysRoleUserService sysRoleUserService;

// 重定向到登录界面
    @RequestMapping("/doLogin")
    public void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getSession().invalidate();
        String path = "sign.jsp";
        response.sendRedirect(path);
    }

// 查询是否存在已注册搅拌站编号
    @RequestMapping("/checkjbzCode")
    @ResponseBody
    public JsonData checkjbzCode( @RequestParam String jbzCode) {
        boolean bool =  sysUserService.checkjbzCode(jbzCode);
        if(bool) {
            return JsonData.fail("--");
        }else{
            return  JsonData.success("--");
        }
    }
// 查询是否存在已注册搅拌站名称
    @RequestMapping("/checkjbzName")
    @ResponseBody
    public JsonData checkjbzName( @RequestParam String jbzname, @RequestParam(value="deptId",required = false) Integer deptId) {

        boolean bool =  sysDeptService.checkExistByName(jbzname,deptId);
        if(bool) {
            return JsonData.fail("--");
        }else{
            return  JsonData.success("--");
        }
    }
//  查询是否存在已注册搅拌站mail
    @RequestMapping("/checkmail")
    @ResponseBody
    public JsonData checkmail( @RequestParam String mail,@RequestParam(value="id",required = false)Integer id) {
        boolean bool =  sysUserService.checkEmailExist(mail,id);
        if(bool) {
            return JsonData.fail("--");
        }else{
            return  JsonData.success("--");
        }
    }

// 查询是否存在已注册搅拌站电话
    @RequestMapping("/checkPhoneNum")
    @ResponseBody
    public JsonData checkPhoneNum( @RequestParam String phoneNum,@RequestParam(value="id",required = false)Integer id) {
        boolean bool =  sysUserService.checkTelephoneExist(phoneNum,id);
        if(bool) {
            return JsonData.fail("--");
        }else{
            return  JsonData.success("--");
        }
    }
//    注册界面
    @RequestMapping("/register.page")
    public ModelAndView page() {
        return new ModelAndView("zhuce");
    }

//  用户信息界面
    @RequestMapping("/userInfo.page")
    @ResponseBody
    public ModelAndView userInfo() {

        return new ModelAndView("userInfo");
    }
//  保存注册用户信息
    @RequestMapping("/saveWebUser")
    @ResponseBody
    public JsonData saveWebUser (UserParam param, Webusercontrol webusercontrol, @RequestParam(value="xlx",required=false) String[] xlx ) {
        int lx  = 0;
        String lxlx = "";
        if(xlx != null){
            for (int i = xlx.length-1;i>-1;i--){
                lxlx = lxlx+xlx[i];
                try {
                    lx= Integer.valueOf(lxlx);
                }catch(NumberFormatException e){
                    e.printStackTrace();
                }catch (IllegalArgumentException e){
                    e.printStackTrace();
                }
            }}
        param.setJbzCode(webusercontrol.getCode());
        param.setStatus(0);
        param.setDeptId(16);
        param.setJbzName(webusercontrol.getMc());
        DeptParam deptParam = new DeptParam();
        deptParam.setName(webusercontrol.getMc());
        webusercontrol.setLx(lx);
        if(webusercontrol.getProvince().equals("天津市")){
            deptParam.setParentId(2);
        }else if(webusercontrol.getProvince().equals("北京市")){
            deptParam.setParentId(3);
        }else {
            deptParam.setParentId(4);
        }
        deptParam.setRemark(webusercontrol.getCode());
        deptParam.setSeq(1);
        if(webusercontrol.getCode() != null && param.getJbzCode() != null&&webusercontrol != null && param != null)
        {
            try {
                int deptId = sysDeptService.save(deptParam);
                param.setDeptId(deptId);
                int sysUserId = sysUserService.save(param);
//                sysRoleUserService.changeRoleUsers(7,new ArrayList<>(sysUserId));
                webUserControlService.save(webusercontrol);
            } catch (Exception e){
                e.printStackTrace();
                logger.error("注册出现异常",e.toString());
                return  JsonData.fail(e.toString());
            }

        }
        return JsonData.success();
    }




}
